package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.datasources.DataSourceNames;
import io.renren.datasources.annotation.DataSource;
import io.renren.modules.ren.dao.RenRecordDao;
import io.renren.modules.ren.entity.RenRecordEntity;
import io.renren.modules.project.service.RenProjectService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("renProjectService")
public class RenProjectServiceImpl extends ServiceImpl<RenRecordDao, RenRecordEntity> implements RenProjectService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public RenRecordEntity getRecord(Map<String, Object> params) throws Exception {
        String useraccount = (String)params.get("projectChargeAccount");
        SysUserEntity userEntity = this.getUserByAccount(useraccount);
        if (userEntity == null) {
            throw new Exception("当前项目负责人没有录入个人信息！");
        }
        RenRecordEntity entity = this.selectOne(
                new EntityWrapper<RenRecordEntity>().eq("user_id", userEntity.getUserId())
        );
        if (StringUtils.isBlank(entity.getMobile()) ) {
            throw new Exception("当前项目负责人没有录入个人手机号！");
        }
        return entity;
    }

    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public RenRecordEntity getRecordByAccount(String useraccount) {
        SysUserEntity userEntity = this.getUserByAccount(useraccount);
        if (userEntity == null) {
            return null;
        }
        RenRecordEntity entity = this.selectOne(
                new EntityWrapper<RenRecordEntity>().eq("user_id", userEntity.getUserId())
        );
        return entity;
    }

    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public SysUserEntity getUserByAccount(String useraccount){
        SysUserEntity userEntity = sysUserService.selectOne(new EntityWrapper<SysUserEntity>().eq("useraccount",useraccount));

        return userEntity;
    }
}